package com.yzjs.localhost.service.hrmPositionalTitles.impl;

import com.alibaba.fastjson.JSONObject;
import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.localhost.dto.ComparisonResultDTO;
import com.yzjs.localhost.dto.DataBundlingDTO;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.entity.hrmContract.HrmContractEntity;
import com.yzjs.localhost.entity.hrmPositionalTitles.HrmPositionalTitlesEntity;
import com.yzjs.localhost.dao.hrmPositionalTitles.HrmPositionalTitlesMapper;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.service.AnalysisService;
import com.yzjs.localhost.service.hrmPositionalTitles.HrmPositionalTitlesService;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("hrmPositionalTitlesService")
public class HrmPositionalTitlesServiceImpl extends BaseServiceImpl<HrmPositionalTitlesEntity, HrmPositionalTitlesMapper > implements HrmPositionalTitlesService {

    @Override
    public ComparisonResultDTO hrmPositionalTitlesCompliance(DataBundlingDTO dto, UserRightsDTO rights) throws Exception {
        TDataPersistenceBeforceEntity beforce =dto.getBeforce();
        String beforceInfo  = beforce.getInfo();
        Map<String,Object> beforceParse = JsonUtil.json2map(beforceInfo);
        TDataPersistenceAfterEntity after = dto.getAfter();
        String afterInfo  = after.getInfo();

        ComparisonResultDTO resultDTO = null ;
        if (beforce.getOperatorCode().contains("add")){
            Map<String,Object>  afterParse = JsonUtil.json2map(afterInfo);
            // 比对 菜单图标  显示顺序 菜单名称 路由地址 菜单类型
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(),beforceParse,afterParse,"posName,titleStatus,titleNum,createBy,titleLevel");
        }else if (beforce.getOperatorCode().contains("edit")){
            Map<String,Object>  afterParse = JsonUtil.json2map(afterInfo);
            // 比对 菜单ID  菜单图标 显示顺序  菜单名称  路由地址 菜单类型
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(),beforceParse,afterParse,"posName,titleStatus,titleId,titleNum,createBy,updateBy,titleLevel,createDate");
        } else if (beforce.getOperatorCode().contains("remove")){
            beforceParse.put("delFlag","2");
            Map<String,Object>  afterParse = JsonUtil.json2map(afterInfo);
            // 比对 菜单ID 状态
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(),beforceParse,afterParse,"titleIds,delFlag");
            if (null==afterParse.get("titleIds")){
                resultDTO.setAbstractName("主键ID为空");
                resultDTO.setWarningInfo("主键ID为空");
                resultDTO.setMathFlag(false);
                return resultDTO;
            }
            HrmPositionalTitlesEntity hrmPositionalTitlesEntity = new HrmPositionalTitlesEntity();
            hrmPositionalTitlesEntity.setTitleId(Integer.valueOf((String) afterParse.get("titleIds")));
            hrmPositionalTitlesEntity.setDelFlag((String) afterParse.get("delFlag"));
            hrmPositionalTitlesEntity.setDataIdentification(after.getDataIdentification());
            this.insert(hrmPositionalTitlesEntity);
            return resultDTO;
        }
        // 记录表表信息
        HrmPositionalTitlesEntity hrmPositionalTitlesEntity = JSONObject.parseObject(after.getInfo(), HrmPositionalTitlesEntity.class);
        hrmPositionalTitlesEntity.setPermissionMirroring(JsonUtil.obj2jsonIgnoreNull(rights));
        hrmPositionalTitlesEntity.setDataIdentification(after.getDataIdentification());
        this.insert(hrmPositionalTitlesEntity);

        return resultDTO;
    }

}